var ProductType = function(){
	
};

ProductType.prototype.initProducts = function(urls) {
	this.urls=urls;
	this.productsTypeList = {};
	this.typeNameSource = new Array();
	var urlWebService=  this.urls["urlBackend"]+ "/ProductTypeService/searchProductTypeByFilter";
	var self = this;
	$.ajax({
		url: urlWebService,
		//data: "{'product':[]}", exampĺe for seding a list as parameter in CXF rest servoce
		data : "_type=json",
		contentType : 'application/json',
		dataType : 'json',
		type : 'GET',
		success: function (data){
			self.initGrid(data);
			self.initButtons();
		}
	});
	
};

ProductType.prototype.generateRow = function (){
	var self=this;
	var urlWebService = self.urls["urlBackend"] + "/ProductTypeService/createTmpObject";
	$.ajax({
		url: urlWebService,
		data : "_type=json",
		contentType : 'application/json',
		dataType : 'json',
		type : 'POST',
		success: function (data){
			var newRow = {
					'model.productType.id' : data.ProductType[0].id,
			        'model.productType.name' : data.ProductType[0].name ,            
			        'model.productType.description' : data.ProductType[0].description
				};
			$("#jqxgrid").jqxGrid('addrow', null, newRow)
		}
	});
};

ProductType.prototype.deleteProductType = function (productId,commit){
	var self = this;
	$("#jqxWidget").block();
	$.ajax({
        url: self.urls["deleteProductType"],
		cache: false,
        data: { 'model.productType.id' : productId},
        success: function (data, status, xhr) {
			commit(true);
			$("#jqxWidget").unblock();
		},
		error: function(jqXHR, textStatus, errorThrown)
		{
			commit(false);
			$("#jqxWidget").unblock();
		}
	});
};

ProductType.prototype.initButtons = function (){
	var self = this;
	$("#addrowbutton,#deleterowbutton").jqxButton({ theme: "darkblue" });
	
	$("#addrowbutton").bind('click', function () {
        var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;
        self.generateRow();
    });
	
	$("#deleterowbutton").bind('click', function () {
        var selectedrowindex = $("#jqxgrid").jqxGrid('getselectedrowindex');
        var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;
        if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
            var id = $("#jqxgrid").jqxGrid('getrowid', selectedrowindex);
            $("#jqxgrid").jqxGrid('deleterow', id);
        }
    });
}

ProductType.prototype.initGrid= function (data){
	var theme = "darkblue";
	var self = this;
	 var source =
    {
        localdata: data,
        datatype: "json",
        addrow: function (rowid, rowdata, position, commit) {
            commit(true);
        },
        deleterow: function (rowid, commit) {
        	self.deleteProductType(rowid,commit);
        },
        updaterow: function (rowid, rowdata, commit) {
  			self.saveProductType(rowid, rowdata, commit);
        },
        datafields:
        [
            { name: 'model.productType.id', type: 'number' , map : 'id' },
            { name: 'model.productType.name', type: 'string' , map : 'name' },            
            { name: 'model.productType.description', type: 'string' , map : 'description' }            
        ],
        id : 'id' ,
        root : "ProductType"
    };
    
    var dataAdapter = new $.jqx.dataAdapter(source);
    
    // initialize jqxGrid
    $("#jqxgrid").jqxGrid(
    {
        width: 600,
        source: dataAdapter,
        editable: true,
        theme: theme,
        editmode: 'dblclick',
        //selectionmode: 'multiplecellsadvanced',
        columns: [
          { text: 'Id', columntype: 'hidden', datafield: 'model.productType.id', width: 40 , editable : false },
          { text: 'Name', columntype: 'textbox', datafield: 'model.productType.name', width: 250 },
          { text: 'Description', columntype: 'textbox', datafield: 'model.productType.description', width: 310 }
        ]
    });
    self.bindGridEvent();
};

ProductType.prototype.saveProductType = function (rowid, rowdata, commit){
	$("#jqxWidget").block();
	var self=this;
   	var dataArray = $.extend({},rowdata); 
   	$.ajax({
   		url: self.urls["saveProductType"],
   		data: dataArray,
   		success: function(data, status, xhr)  {
   			commit(true);		
   			$("#jqxWidget").unblock();
   			$("#cellendeditevent").html("Update <span style='color:blue'><strong>SUCCESS</strong></span>, Column: " + args.datafield + ", Row: " + (1 + args.rowindex) + ", Value: " + args.value);
   			
   		},
   		error: function(jqXHR, textStatus, errorThrown) {
   			commit(false);
   			$("#cellendeditevent").html("Update <span style='color:red'><strong>ERROR</strong></span>, Column: " + args.datafield + ", Row: " + (1 + args.rowindex) + ", Value: " + args.value);
   			$("#jqxWidget").unblock();
   		}
   	});
};

ProductType.prototype.bindGridEvent = function (){
	var self= this;
    // events
    $("#jqxgrid").on('cellendedit', function (event) {
        var args = event.args;
        $("#cellendeditevent").text("Update SUCCESS, Column: " + args.datafield + ", Row: " + (1 + args.rowindex) + ", Value: " + args.value);
    });	
};